// JavaScript Document
function hxsd_tools() {
	var hxsd_tools={

//增加class
		addClass:function (elm, className){
			if(elm.length){
				for(var i=0; i<elm.length;i++){
					elm[i].className+=' '+className;
				}
			}else{
				elm.className+=' '+className;
			}
		},
//删除class
		removeClass:function (elm,className){
			if(elm.length){
				for(var i=0; i<elm.length;i++){
					elm[i].className=elm[i].className.replace(new RegExp(className,'g'),'');
				};
			}else{
				elm.className=elm.className.replace(new RegExp(className,'g'),'');
			};
		},

//累计所有offsetTop
		offsetTop:function ( elements ){
			var top = elements.offsetTop;
			var parent = elements.offsetParent;
			while( parent != null ){
				top += parent.offsetTop;
				parent = parent.offsetParent;
			};
			return top;
		},


//累计所有offsetLeft
		offsetLeft:function ( elements ){
			var left = elements.offsetLeft;
			var parent = elements.offsetParent;
			while( parent != null ){
				left += parent.offsetLeft;
				parent = parent.offsetParent;
			};
			return left;
		},

//判断有没有子集
		isParent:function (oParent,obj){
			while(obj){
				if(obj==oParent)return true; //如果obj==oParent 说明就是‘自己’
				obj=obj.parentNode;	//如果不是，把这个obj指定为obj的父元素
			};
			return false;// 不是我的子级
		},



//居中显示弹框
		popShow:function(elm){
			elm.style.display="block";
			var l=(document.documentElement.clientWidth-elm.offsetWidth)/2;
			var t=(document.documentElement.clientHeight-elm.offsetHeight)/2;
			elm.style.left=l+'px';
			elm.style.top=t+'px';
		},


//拖拽组建
		drag:function(box,title){
			//当我传入1个参数box，拖拽box
			//当我传入2个参数，拖拽就在title
			var handle;
			title?handle=title:handle=box;
//----------------------------------------
//点击事件 title
			handle.onmousedown=function(ev){//按下时机  记录下鼠标的错位位置
				var oEv=ev || window.event;
				var disX=oEv.clientX-box.offsetLeft;//left方向
				var disY=oEv.clientY-box.offsetTop;// top 方向

				//鼠标移动的对象应该是document
				document.onmousemove=function(ev){//移动拖拽
					var oEv=ev || window.event;
					var l=oEv.clientX-disX;
					var t=oEv.clientY-disY;

					//判断屏幕范围
					if(l<0)l=0;
					if(t<0)t=0;
					if(l>document.documentElement.clientWidth-box.offsetWidth)l=document.documentElement.clientWidth-box.offsetWidth;
					if(t>document.documentElement.clientHeight-box.offsetHeight)t=document.documentElement.clientHeight-box.offsetHeight;

					//最后赋值
					box.style.left=l+'px';
					box.style.top=t+'px';
				};

				//释放鼠标move事件
				document.onmouseup=function(){
					document.onmouseup=document.onmousemove=null;
					if(box.releaseCapture)	box.releaseCapture();//取消获捕
				}
				if(box.setCapture)	box.setCapture();//设置捕获
				return false;
			};
		},


//读取样式
		getStyle:function(obj, styleName){
			var value=obj.currentStyle ? obj.currentStyle[styleName]:getComputedStyle(obj, false)[styleName];
			return styleName=='opacity' ? value=Math.round(parseFloat(value)*100):value=parseInt(value);
		},




//运动动画   行动画的样式：opacity  top left  width  height font-size margin  padding-----------------------

		hxsd_move:function (elm,moveJson,speed,fn){
			//预定义速度
			var pr_speed={
				"veryfast":300,
				"fast":500,
				"normal":800,
				"slow":1000,
				"veryslow":2000
			};

			//有没有传speed参数
			if(speed){//是否传入了速度值
				if(typeof speed =='string'){ //如果传入的速度值谁文本
					speed=pr_speed[speed];
				}
			}else{
				speed=pr_speed.normal;
			};

			//----------------------------------------------

			//时间分段
			var count=parseInt(speed/30);

			//距离分段
			//{"width":500,"height":600}
			var start={};
			var dis={};

			for(var key in  moveJson){
				start[key]=this.getStyle(elm, key);
				dis[key]=moveJson[key]-start[key];    //
			}

			var n=0;//计数器

			//------------------------------------------------------
			clearInterval(elm.timer);
			elm.timer=setInterval(function(){
				n++;

				for(var key in moveJson){
					var a=1-n/count;
					//步进长度
					var step_dis=start[key]+ dis[key]*(1-a*a*a);

					//判断透明
					if(key=="opacity"){
						elm.style.filter='alpha(opacity:'+step_dis+')';//ie8以下
						elm.style.opacity=step_dis/100;
					}else{
						elm.style[key]=step_dis+'px';
					}
				};

				//判断运动结束
				if(n==count){
					clearInterval(elm.timer);
					fn && fn();
				}
			},30);
		},

		//以类名获取对象
		getClassName:function (parent,cls){
			if(parent.getElementsByClassName){
				return parent.getElementsByClassName(cls);
			}
			else{
				var arr=[];
				var aElm=parent.getElementsByTagName('*');
				for(var i=0; i<aElm.length;i++){
					if(aElm[i].className.indexOf(cls)!=-1){
						arr.push(aElm[i]);
					}
				}
				return arr;
			}
		},


		picSlide:function(id,hideNum){
			var slide=document.getElementById(id);
			var slideList=slide.getElementsByClassName('slideList')[0];
			var aLi=slideList.getElementsByTagName('li');
			var n=0;//第一张图片的索引
			//ul 设定宽度
			var li_w=aLi[0].offsetWidth;
			slideList.style.width=li_w*aLi.length+'px';
			//插入编号按钮
			var ol=document.createElement('ol');
			for(var i=0; i<aLi.length; i++){
				if(i==0){
					ol.innerHTML+='<li class="ac">'+(hideNum ? "" : (i+1))+'</li>';
				}else{
					ol.innerHTML+='<li>'+(hideNum ? "" : (i+1))+'</li>';
				}
			};
			slide.appendChild(ol);
			//点击数字按钮切换图片
			var aBtn=ol.children;
			function changeBtnClass(){//改变按钮样式
				for(var j=0; j<aBtn.length; j++){
					aBtn[j].className='';
				};
				aBtn[n].className='ac';
			};

			for(var i=0; i<aBtn.length; i++){
				aBtn[i].index=i;//发牌照
				aBtn[i].onclick=function(){
					n=this.index;
					changeBtnClass()
					hxsd_tools.hxsd_move(slideList,{'left':-(li_w*n)});

				};
			};
			var pBtn=slide.getElementsByClassName('prev')[0];
			var nBtn=slide.getElementsByClassName('next')[0];
			pBtn.onclick=function(){
				n--;
				if(n<0)n=0;
				hxsd_tools.hxsd_move(slideList,{'left':-(li_w*n)});
				changeBtnClass();
			};
			nBtn.onclick=function(){
				n++;
				if(n>aLi.length-1)n=aLi.length-1;
				hxsd_tools.hxsd_move(slideList,{'left':-(li_w*n)});
				changeBtnClass();
			}
		}
	}
	return hxsd_tools;
}



